package com.ui;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;

public class AddMetadataColumn {

	private String tableName;
	private String classID;
	private int defCount;
	private StringBuffer buffer = new StringBuffer();
	private String[] ids;
	private int serial;
	private int beginSerial;
	private String defName;

	public AddMetadataColumn(String tableName, String classID, int defCount, int serial, int beginSerial,
			String defName) {
		this.tableName = tableName;
		this.classID = classID;
		this.defCount = defCount;
		this.ids = getIDs();
		this.serial = serial;
		this.beginSerial = beginSerial;
		this.defName = defName;
	}

	private String[] getIDs() {
		String[] ids = new String[this.defCount];
		for (int i = 1; i <= this.defCount; i++) {
			ids[i - 1] = getID();
		}
		return ids;
	}

	private StringBuffer addColumnBf = new StringBuffer();
	private StringBuffer dropColumnBf = new StringBuffer();
	private StringBuffer javaKeyBf = new StringBuffer();
	private StringBuffer javaKeyBf2 = new StringBuffer();
	private StringBuffer javaKeyBf3 = new StringBuffer();
	private StringBuffer javaKeyBf4 = new StringBuffer();
	List<String> defname = new ArrayList<String>();

	private void addTableColumn() {
		String sql = "";
		for (int i = this.beginSerial; i < this.beginSerial + this.defCount; i++) {
			if (i < 10) {
				sql = "alter table " + this.tableName + " add (" + defName + "0" + i + " varchar2(101) default '~');";
				addColumnBf.append(sql);
				addColumnBf.append("\r\n");
				System.out.println(sql);
				defname.add(defName + "0" + i);
				sql = "alter table " + this.tableName + " drop column " + defName + "0" + i + " ;";
				dropColumnBf.append(sql);
				dropColumnBf.append("\r\n");
				System.out.println(sql);
			} else {
				sql = "alter table " + this.tableName + " add (" + defName + "" + i + " varchar2(101) default '~');";
				addColumnBf.append(sql);
				addColumnBf.append("\r\n");
				System.out.println(sql);
				defname.add(defName + i);
				sql = "alter table " + this.tableName + " drop column " + defName + "" + i + " ;";
				dropColumnBf.append(sql);
				dropColumnBf.append("\r\n");
				System.out.println(sql);
			}
		}

		for (String string : defname) {
			javaKeyBf.append("private String ").append(string).append(";");
			javaKeyBf.append(System.lineSeparator());
			javaKeyBf2.append("public static final String ").append(string.toUpperCase()).append(" = \"").append(string)
					.append("\";");
			javaKeyBf2.append(System.lineSeparator());
			javaKeyBf3.append("public void set").append(StrUtil.upperFirst(string)).append("(String ").append(string)
					.append(") {").append("this.").append(string).append(" = ").append(string).append("; }");
			javaKeyBf4.append(System.lineSeparator());
			javaKeyBf3.append("public void get").append(StrUtil.upperFirst(string)).append("() { return this.")
					.append(string).append("; }");
			javaKeyBf3.append(System.lineSeparator());
			javaKeyBf4.append("public void set").append(StrUtil.upperFirst(string)).append("(String ").append(string)
					.append(") {").append(" setAttributeValue(\"").append(string).append("\",").append(string)
					.append("); }");
			javaKeyBf4.append(System.lineSeparator());
			javaKeyBf4.append("public void get").append(StrUtil.upperFirst(string))
					.append("() { return (String)getAttributeValue(\"").append(string).append("\"); }");
			javaKeyBf4.append(System.lineSeparator());
		}

	}

	StringBuffer defbf = new StringBuffer();

	private String[] numbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	private String getSubstr(int len) {
		String str = "";
		for (int i = 0; i < len; i++) {
			int ran = (int) (Math.random() * 15);
			str += numbers[ran];
		}
		return str;
	}

	private String getID() {
		String str = getSubstr(8) + "-" + getSubstr(4) + "-" + getSubstr(4) + "-" + getSubstr(4) + "-" + getSubstr(12);
		return str;
	}

	private void addProperty() {
		String sql = "";
		for (int i = this.beginSerial; i < this.beginSerial + this.defCount; i++) {
			if (i < 10) {
				sql = "insert into md_property (ACCESSORCLASSNAME, ACCESSPOWER, ACCESSPOWERGROUP, ATTRLENGTH, ATTRMAXVALUE, ATTRMINVALUE, ATTRSEQUENCE, CALCULATION, CLASSID, CREATEINDUSTRY, CREATETIME, CREATOR, CUSTOMATTR, DATATYPE, DATATYPESTYLE, DEFAULTVALUE, DESCRIPTION, DISPLAYNAME, DR, DYNAMICATTR, DYNAMICTABLE, FIXEDLENGTH, HELP, HIDED, ID, INDUSTRY, ISACTIVE, ISAUTHEN, MODIFIER, MODIFYTIME, NAME, NOTSERIALIZE, NULLABLE, PRECISE, READONLY, REFMODELNAME, RESID, TS, VERSIONTYPE, VISIBILITY)"
						+ "values (null, 'N', null, 101, null, null, " + (this.serial + i - this.beginSerial + 1)
						+ ", 'N', '" + this.classID
						+ "', null, null, null, 'Y', 'BS000010000100001056', 300, null, null, '自定义项" + i
						+ "', 0, 'N', null, 'N', null, 'N', '" + ids[i - this.beginSerial]
						+ "', '0', 'Y', 'Y', null, null, '" + defName + "0" + "" + i
						+ "', 'N', 'Y', 0, 'N', null, null, '2019-08-14 20:41:36', 0, 0);" + "";
				buffer.append(sql);
				buffer.append("\r\n");
				System.out.println(sql);

				sql = "delete from md_property where id = '" + ids[i - this.beginSerial] + "';";
				defbf.append(sql);
				defbf.append("\r\n");
				System.out.println(sql);
			} else {
				sql = "insert into md_property (ACCESSORCLASSNAME, ACCESSPOWER, ACCESSPOWERGROUP, ATTRLENGTH, ATTRMAXVALUE, ATTRMINVALUE, ATTRSEQUENCE, CALCULATION, CLASSID, CREATEINDUSTRY, CREATETIME, CREATOR, CUSTOMATTR, DATATYPE, DATATYPESTYLE, DEFAULTVALUE, DESCRIPTION, DISPLAYNAME, DR, DYNAMICATTR, DYNAMICTABLE, FIXEDLENGTH, HELP, HIDED, ID, INDUSTRY, ISACTIVE, ISAUTHEN, MODIFIER, MODIFYTIME, NAME, NOTSERIALIZE, NULLABLE, PRECISE, READONLY, REFMODELNAME, RESID, TS, VERSIONTYPE, VISIBILITY)"
						+ "values (null, 'N', null, 101, null, null, " + (this.serial + i - this.beginSerial + 1)
						+ ", 'N', '" + this.classID
						+ "', null, null, null, 'Y', 'BS000010000100001056', 300, null, null, '自定义项" + i
						+ "', 0, 'N', null, 'N', null, 'N', '" + ids[i - this.beginSerial]
						+ "', '0', 'Y', 'Y', null, null, '" + defName + "" + i
						+ "', 'N', 'Y', 0, 'N', null, null, '2019-08-14 20:41:36', 0, 0);" + "";
				buffer.append(sql);
				buffer.append("\r\n");
				System.out.println(sql);

				sql = "delete from md_property where id = '" + ids[i - this.beginSerial] + "';";
				defbf.append(sql);
				defbf.append("\r\n");
				System.out.println(sql);
			}
		}
	}

	StringBuffer xmlBf = new StringBuffer();

	private void addPropertyXML() {
		String sql = "";
		for (int i = this.beginSerial; i < this.beginSerial + this.defCount; i++) {
			if (i < 10) {
				sql = "<attribute " + "accessStrategy=\"\" " + "accesspower=\"false\" " + "accesspowergroup=\"\" "
						+ "calculation=\"false\" " + "classID=\"" + this.classID + "\" " + "createIndustry=\"0\" "
						+ "dataType=\"BS000010000100001056\" " + "dataTypeStyle=\"SINGLE\" " + "dbtype=\"varchar\" "
						+ "defaultValue=\"\" " + "description=\"\" " + "displayName=\"自定义项0" + i + "\" "
						+ "dynamic=\"false\" " + "dynamicTable=\"\" " + "fieldName=\"" + defName + "0" + i + "\" "
						+ "fieldType=\"varchar\" " + "fixedLength=\"false\" " + "forLocale=\"false\" " + "help=\"\" "
						+ "id=\"" + ids[i - this.beginSerial] + "\" " + "industryChanged=\"false\" "
						+ "isActive=\"true\" " + "isAuthorization=\"true\" " + "isDefaultDimensionAttribute=\"false\" "
						+ "isDefaultMeasureAttribute=\"false\" " + "isFeature=\"true\" " + "isGlobalization=\"false\" "
						+ "isHide=\"false\" " + "isKey=\"false\" " + "isNullable=\"true\" " + "isReadOnly=\"false\" "
						+ "isShare=\"false\" " + "isSource=\"false\" " + "length=\"101\"" + " maxValue=\"\" "
						+ "minValue=\"\" " + "modifyIndustry=\"0\" " + "name=\"" + defName + "0" + i + "\" "
						+ "notSerialize=\"false\" " + "precise=\"\" " + "refModelName=\"\" " + "resid=\"\" "
						+ "sequence=\"" + (this.serial + i - this.beginSerial + 1) + "\" " + "typeDisplayName=\"自定义项\" "
						+ "typeName=\"CUSTOM\" " + "versionType=\"0\" " + "visibility=\"public\"/>";
				xmlBf.append(sql);
				xmlBf.append("\r\n");
				System.out.println(sql);

			} else {
				sql = "<attribute " + "accessStrategy=\"\" " + "accesspower=\"false\" " + "accesspowergroup=\"\" "
						+ "calculation=\"false\" " + "classID=\"" + this.classID + "\" " + "createIndustry=\"0\" "
						+ "dataType=\"BS000010000100001056\" " + "dataTypeStyle=\"SINGLE\" " + "dbtype=\"varchar\" "
						+ "defaultValue=\"\" " + "description=\"\" " + "displayName=\"自定义项" + i + "\" "
						+ "dynamic=\"false\" " + "dynamicTable=\"\" " + "fieldName=\"" + defName + "" + i + "\" "
						+ "fieldType=\"varchar\" " + "fixedLength=\"false\" " + "forLocale=\"false\" " + "help=\"\" "
						+ "id=\"" + ids[i - this.beginSerial] + "\" " + "industryChanged=\"false\" "
						+ "isActive=\"true\" " + "isAuthorization=\"true\" " + "isDefaultDimensionAttribute=\"false\" "
						+ "isDefaultMeasureAttribute=\"false\" " + "isFeature=\"true\" " + "isGlobalization=\"false\" "
						+ "isHide=\"false\" " + "isKey=\"false\" " + "isNullable=\"true\" " + "isReadOnly=\"false\" "
						+ "isShare=\"false\" " + "isSource=\"false\" " + "length=\"101\"" + " maxValue=\"\" "
						+ "minValue=\"\" " + "modifyIndustry=\"0\" " + "name=\"" + defName + "" + i + "\" "
						+ "notSerialize=\"false\" " + "precise=\"\" " + "refModelName=\"\" " + "resid=\"\" "
						+ "sequence=\"" + (this.serial + i - this.beginSerial + 1) + "\" " + "typeDisplayName=\"自定义项\" "
						+ "typeName=\"CUSTOM\" " + "versionType=\"0\" " + "visibility=\"public\"/>";
				xmlBf.append(sql);
				xmlBf.append("\r\n");
				System.out.println(sql);

			}
		}
	}

	private void addOrmap() {
		String sql = "";
		for (int i = this.beginSerial; i < this.beginSerial + this.defCount; i++) {
			if (i < 10) {
				sql = "insert into md_ormap (ATTRIBUTEID, CLASSID, COLUMNID, DR, TABLEID, TS)" + "values ('"
						+ ids[i - this.beginSerial] + "', '" + this.classID + "', '" + this.tableName + "@@@" + defName
						+ "0" + i + "', 0, '" + this.tableName + "', '2019-08-14 20:41:36');" + "";
				buffer.append(sql);
				buffer.append("\r\n");
				System.out.println(sql);

				sql = "delete from md_ormap where ATTRIBUTEID = '" + ids[i - this.beginSerial] + "';";
				defbf.append(sql);
				defbf.append("\r\n");
				System.out.println(sql);
			} else {
				sql = "insert into md_ormap (ATTRIBUTEID, CLASSID, COLUMNID, DR, TABLEID, TS)" + "values ('"
						+ ids[i - this.beginSerial] + "', '" + this.classID + "', '" + this.tableName + "@@@" + defName
						+ "" + i + "', 0, '" + this.tableName + "', '2019-08-14 20:41:36');" + "";
				buffer.append(sql);
				buffer.append("\r\n");
				System.out.println(sql);

				sql = "delete from md_ormap where ATTRIBUTEID = '" + ids[i - this.beginSerial] + "';";
				defbf.append(sql);
				defbf.append("\r\n");
				System.out.println(sql);
			}
		}
	}

	private void addColumn() {
		String sql = "";
		for (int i = this.beginSerial; i < this.beginSerial + this.defCount; i++) {
			if (i < 10) {
				sql = "insert into md_column (COLUMNLENGTH, COLUMNSEQUENCE, COLUMNTYPE, CREATETIME, CREATOR, DEFAULTVALUE, DESCRIPTION, DISPLAYNAME, DR, FORLOCALE, GROUPID, HELP, ID, IDENTITIED, INCREMENTSEED, INCREMENTSTEP, ISACTIVE, MODIFIER, MODIFYTIME, NAME, NULLABLE, PKEY, PRECISE, RESID, SQLDATETYPE, TABLEID, TS, VERSIONTYPE)"
						+ "values (101, " + (this.serial + i - this.beginSerial + 1)
						+ ", 0, null, null, null, null, '自定义项" + "" + i + "', 0, 'N', null, null, '" + this.tableName
						+ "@@@" + defName + "0" + i + "', null, null, null, 'Y', null, null, '" + defName + "0" + i
						+ "', 'Y', 'N', 0, null, 'varchar', '" + this.tableName + "', '2019-08-14 20:41:36', 0);" + "";
				buffer.append(sql);
				buffer.append("\r\n");
				System.out.println(sql);

				sql = "delete from md_column where ID = '" + this.tableName + "@@@" + defName + "0" + i + "';";
				defbf.append(sql);
				defbf.append("\r\n");
				System.out.println(sql);
			} else {
				sql = "insert into md_column (COLUMNLENGTH, COLUMNSEQUENCE, COLUMNTYPE, CREATETIME, CREATOR, DEFAULTVALUE, DESCRIPTION, DISPLAYNAME, DR, FORLOCALE, GROUPID, HELP, ID, IDENTITIED, INCREMENTSEED, INCREMENTSTEP, ISACTIVE, MODIFIER, MODIFYTIME, NAME, NULLABLE, PKEY, PRECISE, RESID, SQLDATETYPE, TABLEID, TS, VERSIONTYPE)"
						+ "values (101, 15, 0, null, null, null, null, '自定义项" + i + "', 0, 'N', null, null, '"
						+ this.tableName + "@@@" + defName + "" + i + "', null, null, null, 'Y', null, null, '"
						+ defName + "" + i + "', 'Y', 'N', 0, null, 'varchar', '" + this.tableName
						+ "', '2019-08-14 20:41:36', 0);" + "";
				buffer.append(sql);
				buffer.append("\r\n");
				System.out.println(sql);

				sql = "delete from md_column where ID = '" + this.tableName + "@@@" + defName + i + "';";
				defbf.append(sql);
				defbf.append("\r\n");
				System.out.println(sql);
			}
		}
	}

	public void generateScript() {
		addTableColumn();
		addProperty();
		addOrmap();
		addColumn();
		addPropertyXML();
	}

	public StringBuffer getIsnertScript() {

		return buffer;
	}

	public StringBuffer getScriptXML() {

		return xmlBf;
	}

	public StringBuffer getDelScript() {
		return defbf;
	}

	public StringBuffer getAddCloumnScript() {
		return addColumnBf;
	}

	public StringBuffer getDropCloumnScript() {
		return dropColumnBf;
	}
	
	public StringBuffer getJavaKey() {
		StringBuffer result = new StringBuffer();
		result.append(javaKeyBf);
		result.append(javaKeyBf2);
		result.append(javaKeyBf3);
		return result;
	}
	
	public StringBuffer getJavaKey2() {
		StringBuffer result = new StringBuffer();
		result.append(javaKeyBf);
		result.append(javaKeyBf2);
		result.append(javaKeyBf4);
		return result;
	}

}